<?php

(defined('BASEPATH')) OR exit('No direct script access allowed');

/* load the MX_Loader class */
require APPPATH . "third_party/MX/Controller.php";

class MY_Controller extends MX_Controller {

    /**
     * Holding class instance
     * @var MY_Controller 
     */
    private static $instance = null;

    /**
     * Holding view variables
     * @var array
     */
    public $view_vars = array();

    /**
     * Default layout name
     */
    public $layout_name = 'default';

    /**
     * Disable layout
     * @var boolean
     */
    public $disable_layout = false;

    /**
     * Disable view
     * @var type 
     */
    public $disable_view = false;

    /**
     * Constructor
     */
    public function __construct()
    {
        parent::__construct();
        if(file_exists(APPPATH.'/inc/config.php'))
            require_once(APPPATH.'/inc/config.php');
        else exit('Not found config file');
        $this->load->model('categories');
        global $menus ;
        $menus= $this->categories->getCat(array('is_menu'=>1));
        global $site_info;
        $site_info = $config;
        $this->load->model('languages');
        $this->_layout = null;
        $this->_admin_sidebar = array('category'=>array('Shop'
                                                        ,'Article'
                                                        ,'Profile'
                                                        ,'Gallery'
                                                        ,'Slideshow'
                                                        ,'Link'
                                                        ,'Video'
                                                        ,'Header')
                                        ,'shop'=>array('Product'
                                                    ,'Order'
                                                    ,'Promotion'
                                                    ,'Comment')
                                    ,'article'=>array('Posts')
                                    
                                    ,'banner'=>array('Index')
                                    ,'user'=>array('User')
                                    ,'setting'=>array('General','Country','Language','Translate'));
        $module_name = $this->router->fetch_module();
        $controller_name = $this->router->fetch_module();
        $method_name = $this->router->fetch_method();
        $this->assign('admin_sidebars',$this->_admin_sidebar);
        //Load layout config for module
        $this->config->load('layouts', true);

        ($layout_name = $this->config->item($module_name . '/' . $controller_name . '/' . $method_name, 'layouts')) ||
        ($layout_name = $this->config->item($module_name . '/' . $controller_name, 'layouts')) ||
        ($layout_name = $this->config->item($module_name, 'layouts'));
        //Load default Language
        $this->assign('languages',$this->languages->get_language(false,1));
        if(!$this->session->userdata('lan'))
        $this->session->set_userdata('lan',$this->languages->get_language_default(true)->lang_code);
        $this->load->language('global',$this->session->userdata('lan'));
        //If layout is defined
        if ($layout_name)
        {
            $this->layout_name = $layout_name;
        }

        //Assign logged in user info to all views
      /*  $this->load->vars(array(
            '_globals' => array(
                'user' => $this->tank_auth->get_user(),
                'module_name' => $module_name,
                'controller_name' => $controller_name,
                'method_name' => $method_name,                
            )
        ));*/

        //Init self instance
        self::$instance = & $this;
    }

    /**
     * Return self instance
     * @return object 
     */
    public static final function get_instance()
    {
        return self::$instance;
    }

    /**
     * Assign value to view
     * @param string $key
     * @param mix $value 
     */
    protected function assign($key, $value)
    {
        $this->view_vars[$key] = $value;
    }

    /**
     * Disable layout
     * @param boolean $disable 
     */
    protected function disable_layout($disable = true)
    {
        $this->disable_layout = $disable;
    }

    /**
     * Disable view
     * @param boolean $disable 
     */
    protected function disable_view($disable = true)
    {
        $this->disable_view = $disable;
    }

    /**
     * Set layout
     * @param string $layout_name 
     */
    public function set_layout($layout_name)
    {
        $this->layout_name = $layout_name;
    }
    
     public function valid_token($show_error=FALSE)
    {
        $this->load->helper('cookie');
        
            $post_token = $this->input->post('csrf_token',TRUE);
            $csrf_token = get_cookie('csrf_token');
            
            if($post_token == $csrf_token)
            {
                return TRUE ;
            }    
       
        if($show_error)
        {
            show_error('invalid_token');
        }
        return FALSE;
    }
       

}